<template>
    <a-modal :confirm-loading="modalOption.loading" :okText="modalOption.okText" @ok="modalOption.onOk"
             @cancel="modalOption.onCancel" :cancelText="modalOption.cancelText" :width="modalOption.width" :visible="visible"
             :title="modalOption.title">
        <a-spin tip="loading" :spinning="loading">
            <div class="container">
                <a-form-model ref="formModel" :label-col="formOption.labelCol" :wrapper-col="formOption.wrapperCol"
                              :layout="formOption.layout" :label-align="formOption.labelAlign" :hide-required-mark="formOption.hideRequiredMark"
                              :rules="formOption.rules" :model="formOption.formData">
                    <div v-if="tabName==='饮片库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select :disabled="viewDetail || tabName!='饮片库存表'"  @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="ypName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange" 
                                        :disabled="viewDetail || tabName!='饮片库存表'" 
                                        v-model="formOption.formData.ypName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="物料码" prop="materialCode">
                                        <a-input v-if="formOption.formData.ypName" disabled v-model="formOption.formData.materialCode" placeholder="物料码" />
                                        <span v-else>请选择品名</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="生产类型" prop="prodType" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select :disabled="viewDetail || tabName!='饮片库存表'"  v-model="formOption.formData.prodType" placeholder="生产类型">
                                            <a-select-option value="CW">CW</a-select-option>
                                            <a-select-option value="SY">SY</a-select-option>
                                            <a-select-option value="G">G</a-select-option>
                                            <a-select-option value="ZCW">ZCW</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="生产流水号" prop="prodNo">
                                        <a-input :disabled="viewDetail || tabName!='饮片库存表'"  v-model="formOption.formData.prodNo" placeholder="生产流水号" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="ypBatch">
                                        <a-input :disabled="viewDetail" v-model="formOption.formData.ypBatch" placeholder="饮片批号" />
                                        <div v-if="!formOption.formData.prodType || !materialCode || !formOption.formData.prodNo" style="color: #ff4d4f; font-size: 12px; margin-top: 4px;">
                                            {{ !formOption.formData.productionType ? '请选择生产类型' : 
                                               !materialCode ? '请选择物料码' : 
                                               !formOption.formData.prodNo ? '请填写生产流水号' : '' }}
                                        </div>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="ypRemark">
                                        <a-input :disabled="viewDetail" v-model="formOption.formData.ypRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="12">
                                    <a-form-model-item label="入库日期" prop="ypInTime">
                                        <a-date-picker :disabled="viewDetail" v-model="formOption.formData.ypInTime" valueFormat="YYYY-MM-DD" placeholder="入库日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="12">
                                    <a-form-model-item label="入库重量(kg)" prop="ypInWeight">
                                        <a-input :disabled="viewDetail" v-model="formOption.formData.ypInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="24">
                                    <a-form-model-item label="结存重量(kg)" prop="ypBalanceWeight">
                                        <a-input  v-model="formOption.formData.ypBalanceWeight" placeholder="结存重量" disabled />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                    <div v-if="tabName==='收粉库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select :disabled="formOption.formData.sfBalanceWeight ? true:false" @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="tqwName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        :disabled="formOption.formData.sfBalanceWeight ? true:false"
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange" 
                                        v-model="formOption.formData.tqwName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="tqwBatch" :rules="{required:true,message:correctName+'批号不能为空'}">
                                        <a-input :disabled="formOption.formData.sfBalanceWeight ? true:false" v-model="formOption.formData.tqwBatch" @change="getBalanceData" :placeholder="correctName+'批号'" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="sfRemark">
                                        <a-input :disabled="formOption.formData.sfBalanceWeight ? true:false" v-model="formOption.formData.sfRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="入库日期" prop="sfInTime" :rules="[{ required: true, message: '入库日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.sfBalanceWeight ? true:false" v-model="formOption.formData.sfInTime" valueFormat="YYYY-MM-DD" placeholder="入库日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="入库重量(kg)" prop="sfInWeight">
                                        <a-input :disabled="formOption.formData.sfBalanceWeight ? true:false" v-model="formOption.formData.sfInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="记录重量(kg)" prop="recordWeight" :rules="[{ required: false, message: '记录重量不能为空' }]">
                                        <a-input :disabled="true" v-model="formOption.formData.recordWeight" placeholder="记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="结存重量(kg)" prop="sfBalanceWeight">
                                        <a-input disabled v-model="formOption.formData.sfBalanceWeight" placeholder="结存重量" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="结存记录重量(kg)" prop="storageRecordWeight">
                                        <a-input disabled v-model="formOption.formData.storageRecordWeight" placeholder="结存记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                    <div v-if="tabName==='过筛库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select disabled  @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="tqwName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange" 
                                        disabled
                                        v-model="formOption.formData.tqwName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="tqwBatch">
                                        <a-input disabled v-model="formOption.formData.tqwBatch" :placeholder="correctName+'批号'" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="gsRemark">
                                        <a-input :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="入库日期" prop="gsInTime" :rules="[{ required: true, message: '入库日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsInTime" valueFormat="YYYY-MM-DD" placeholder="入库日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="件数" prop="gsNum" :rules="[{ required: true, message: '件数不能为空' }]">
                                        <a-input-number style="width: 200px;" :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsNum" placeholder="件数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="单位(kg/件)" prop="gsSpec" :rules="[{ required: true, message: '单位不能为空' }]">
                                        <a-input-number style="width: 200px;" :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsSpec" placeholder="件数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                <a-form-model-item label="零件(kg)" prop="gsPart" :rules="[{ required: true, message: '零件不能为空' }]">
                                        <a-input-number style="width: 200px;" :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsPart" placeholder="零件" />
                                </a-form-model-item>
                            </a-col>
                            </a-row>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="入库重量(kg)" prop="gsInWeight">
                                        <a-input :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="gsInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="记录重量(kg)" prop="recordWeight" :rules="[{ required: false, message: '记录重量不能为空' }]">
                                        <a-input :disabled="true" v-model="formOption.formData.recordWeight" placeholder="记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="结存重量(kg)" prop="gsBalanceWeight">
                                        <a-input disabled v-model="formOption.formData.gsBalanceWeight" placeholder="结存重量" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="过筛后粉位置" prop="gsPosition" :rules="[{ required: true, message: '过筛后粉位置不能为空' }]">
                                        <a-input :disabled="formOption.formData.gsBalanceWeight?true:false" v-model="formOption.formData.gsPosition" placeholder="过筛后粉位置" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="结存记录重量(kg)" prop="storageRecordWeight">
                                        <a-input disabled v-model="formOption.formData.storageRecordWeight" placeholder="结存记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                    <div v-if="tabName==='总混一库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select disabled  @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="tqwName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange" 
                                        disabled
                                        v-model="formOption.formData.tqwName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="tqwBatch">
                                        <a-input disabled v-model="formOption.formData.tqwBatch" :placeholder="correctName+'批号'" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="zhyRemark">
                                        <a-input :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="入库日期" prop="zhyInTime" :rules="[{ required: true, message: '入库日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyInTime" valueFormat="YYYY-MM-DD" placeholder="入库日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="入库重量(kg)" prop="zhyInWeight" :rules="[{ required: true, message: '入库重量不能为空' }]">
                                        <a-input :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="GMP报告数" prop="zhyGmpNum" :rules="[{ required: true, message: 'GMP报告数不能为空' }]">
                                        <a-input :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyGmpNum" placeholder="GMP报告数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="失效期(月)" prop="zhyExpiration" :rules="[{ required: true, message: '失效期不能为空' }]">
                                        <a-input :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyExpiration" placeholder="失效期" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="记录重量(kg)" prop="recordWeight" :rules="[{ required: false, message: '记录重量不能为空' }]">
                                        <a-input :disabled="true" v-model="formOption.formData.recordWeight" placeholder="记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="结存重量(kg)" prop="zhyBalanceWeight">
                                        <a-input v-model="formOption.formData.zhyBalanceWeight" placeholder="结存重量" disabled />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="总混一后位置" prop="zhyPosition" :rules="[{ required: true, message: '总混一后位置不能为空' }]">
                                        <a-input :disabled="formOption.formData.zhyBalanceWeight?true:false" v-model="formOption.formData.zhyPosition" placeholder="总混一后位置" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="结存记录重量(kg)" prop="storageRecordWeight">
                                        <a-input disabled v-model="formOption.formData.storageRecordWeight" placeholder="结存记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                    <div v-if="tabName==='总混二库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select :disabled="formOption.formData.zheBalanceWeight?true:false" @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="zlName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange"
                                        :disabled="formOption.formData.zheBalanceWeight?true:false"
                                        v-model="formOption.formData.zlName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="提取物批号" prop="tqwId" :rules="[{ required: true, message: '批号不能为空' }]">
                                        <a-select
                                        show-search
                                        @change="getBalanceData"
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        :disabled="formOption.formData.zheBalanceWeight?true:false"
                                        v-model="formOption.formData.tqwId" 
                                        placeholder="提取物批号">
                                            <a-select-option v-for="item in searchSource.zhyList" :key="item.id" :value="item.tqwId">{{ item.tqwName }}-{{ item.tqwBatch }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="zlBatch">
                                        <a-input :disabled="formOption.formData.zheBalanceWeight?true:false" v-model="formOption.formData.zlBatch" @change="getBalanceData" :placeholder="correctName+'批号'" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="zheRemark">
                                        <a-input :disabled="formOption.formData.zheBalanceWeight?true:false" v-model="formOption.formData.zheRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="入库日期" prop="zheInTime" :rules="[{ required: true, message: '入库日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.zheBalanceWeight?true:false" v-model="formOption.formData.zheInTime" valueFormat="YYYY-MM-DD" placeholder="入库日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="入库重量(kg)" prop="zheInWeight" :rules="[{ required: true, message: '入库重量不能为空' }]">
                                        <a-input :disabled="formOption.formData.zheBalanceWeight?true:false" v-model="formOption.formData.zheInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="记录重量(kg)" prop="recordWeight" :rules="[{ required: false, message: '记录重量不能为空' }]">
                                        <a-input :disabled="true" v-model="formOption.formData.recordWeight" placeholder="记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="结存重量(kg)" prop="zheBalanceWeight">
                                        <a-input  v-model="formOption.formData.zheBalanceWeight" placeholder="结存重量" disabled />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="结存记录重量(kg)" prop="storageRecordWeight">
                                        <a-input disabled v-model="formOption.formData.storageRecordWeight" placeholder="结存记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                    <div v-if="tabName==='制粒库存表'">
                        <!-- 基本信息 -->
                        <a-card title="基本信息" hoverable>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="标准类型" prop="standard" :rules="[{ required: true, message: '标准类型不能为空' }]">
                                        <a-select disabled  @change="standardTypeChange" v-model="formOption.formData.standard" placeholder="标准类型">
                                            <a-select-option value="国标">国标</a-select-option>
                                            <a-select-option value="省标">省标</a-select-option>
                                            <a-select-option value="省标-北京">省标-北京</a-select-option>
                                            <a-select-option value="省标-上海">省标-上海</a-select-option>
                                            <a-select-option value="其他">其他</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="品名" prop="zlName" :rules="[{ required: true, message: '品名不能为空' }]">
                                        <a-select
                                        show-search
                                        :filter-option="$filterOption"
                                        v-if="formOption.formData.standard" 
                                        @change="materialNameChange" 
                                        disabled
                                        v-model="formOption.formData.zlName" 
                                        placeholder="品名">
                                            <a-select-option v-for="item in searchSource.materialNameList" :key="item.id" :value="item.name">{{ item.name }}</a-select-option>
                                        </a-select>
                                        <span v-else>请先选择标准类型</span>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item :label="correctName+'批号'" prop="zlBatch" :rules="{required:true,message:correctName+'批号不能为空'}">
                                        <a-input disabled v-model="formOption.formData.zlBatch" :placeholder="correctName+'批号'"/>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="备注" prop="zlRemark">
                                        <a-input :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlRemark" placeholder="备注" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 入库信息 -->
                        <a-card title="入库信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="制粒产出日期" prop="zlProdTime" :rules="[{ required: true, message: '制粒产出日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlProdTime" valueFormat="YYYY-MM-DD" placeholder="制粒产出日期" style="width: 100%" />
                                    </a-form-model-item>
                                </a-col>
                               
                                <a-col :span="6">
                                    <a-form-model-item label="件数" prop="zlNum" :rules="[{ required: true, message: '件数不能为空' }]">
                                        <a-input :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlNum" placeholder="件数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="单位(kg/件)" prop="zlSpec" :rules="[{ required: true, message: '单位不能为空' }]">
                                        <a-input :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlSpec" placeholder="件数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="零件(kg)" prop="zlPart" :rules="[{ required: true, message: '零件不能为空' }]">
                                        <a-input :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlPart" placeholder="零件" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                
                                <a-col :span="6">
                                    <a-form-model-item label="入库重量(kg)" prop="zlInWeight" :rules="[{ required: true, message: '入库重量不能为空' }]">
                                        <a-input disabled v-model="zlInWeight" placeholder="入库重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                                
                                
                                <a-col :span="6">
                                    <a-form-model-item label="u8入账" prop="zlU8" :rules="[{ required: true, message: 'u8入账不能为空' }]">
                                        <a-select :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlU8" placeholder="u8入账件">
                                            <a-select-option value="已入">已入</a-select-option>
                                            <a-select-option value="未入">未入</a-select-option>
                                        </a-select>
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="GMP报告数" prop="zlGmpNum" :rules="[{ required: true, message: 'GMP报告数不能为空' }]">
                                        <a-input :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlGmpNum" placeholder="GMP报告数" />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="记录重量(kg)" prop="recordWeight" :rules="[{ required: false, message: '记录重量不能为空' }]">
                                        <a-input :disabled="true" v-model="formOption.formData.recordWeight" placeholder="记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="生产日期" prop="zlTime" :rules="[{ required: true, message: '生产日期不能为空' }]">
                                        <a-date-picker :disabled="formOption.formData.zlBalanceWeight?true:false" v-model="formOption.formData.zlTime" valueFormat="YYYY-MM-DD" placeholder="入库时间" style="width: 100%" />
                                        <div style="color: #ff4d4f;">生产日期并非制粒产出日期,请注意填写</div>
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 结存信息 -->
                        <a-card title="结存信息" hoverable style="margin-top: 16px;">
                            <a-row>
                                <a-col :span="6">
                                    <a-form-model-item label="结存重量(kg)" prop="zlBalanceWeight">
                                        <a-input v-model="formOption.formData.zlBalanceWeight" placeholder="结存重量" disabled />
                                    </a-form-model-item>
                                </a-col>
                                <a-col :span="6">
                                    <a-form-model-item label="结存记录重量(kg)" prop="storageRecordWeight">
                                        <a-input disabled v-model="formOption.formData.storageRecordWeight" placeholder="结存记录重量(kg)" />
                                    </a-form-model-item>
                                </a-col>
                            </a-row>
                        </a-card>
                        <!-- 库存调整记录 -->
                        <a-card title="库存调整记录" hoverable style="margin-top: 16px;">
                            <a-table :columns="adjustColumns" :dataSource="formOption.formData.nonProdEditRecordList" rowKey="index" bordered size="small" :pagination="false" />
                        </a-card>
                    </div>
                </a-form-model>
            </div>
            <div slot="footer" style="text-align: center">
                <a-button size="large" @click="modalOption.onCancel">取 消</a-button>
                <a-button size="large" type="primary" @click="modalOption.onOk">确 定</a-button>
            </div>
        </a-spin>
    </a-modal>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";
import nextApi from "@/api/nextApi";
import { getStorageUserInfo } from "@/utils";
import tools from "@/utils/tools";
@Component({})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: "" }) editId!: string;
    @Prop({ default: "" }) tabName!: string;
    @Prop({ default: "" }) formData!: object;
    @Prop({ default: false}) viewDetail!: boolean
    @Watch("visible")
    visibleChange(val) {
        if (val) {
            this.searchSource = {
                materialNameList: [],
            };
        } else {
            //关闭弹窗初始化数据组
            this.searchSource = {
                materialNameList: [],
            };
            if (this.$refs.formModel) {
                (this.$refs.formModel as any).resetFields();
            }
            Object.keys(this.formOption.formData).forEach(key => {
            this.formOption.formData = {
                ypBatch:''
            }
        });
        }
    }
    materialCode = ''
    
    loading = false
    @Watch("editId",{immediate:true})
    editIdChange(val) {
        if (val) {
            console.log('id:',val)
            this.modalOption.title = `编辑-${this.tabName || ''}`;
            this.formOption.formData = this.formData || {};
            if(this.tabName!= '饮片库存表'){
                this.getBalanceTableData()
                if(this.tabName==='总混一库存表' && !this.formOption.formData.zhyBalanceWeight){
                    nextApi.getMaterialList({name:this.formOption.formData.tqwName}).then(res=>{
                        this.formOption.formData.zhyExpiration = res.data.records[0].validDay
                    })
                }
            }
            // 处理饮片库存表的批号回填
            if (this.tabName === '饮片库存表' && this.formOption.formData.ypBatch) {
                const batch = this.formOption.formData.ypBatch;
                // 从批号中提取生产类型、物料码和生产流水号
                this.formOption.formData.prodType = batch.substring(0, 2);
                this.materialCode = batch.substring(2, 8);
                this.formOption.formData.prodNo = batch.substring(8);
            }
        } else {
            this.modalOption.title = `新增-${this.tabName || ''}`;
        }
    }
    @Watch('tabName')
    tabNameChange(){
        this.editIdChange(this.editId)
    }
    //组件配置
    modalOption: any = {
        locale: locale,
        width: "90%",
        title: "新增",
        okText: "保存",
        cancelText: "取消",
        loading: false,
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };

    //表单数据源
    searchSource:any = {
        materialNameList:[],//成品列表也就是查的产品管理的接口
        zhyList:[],
    }
    //表单配置
    formOption: any = {
        labelCol: { style: { width: "140px" } },
        wrapperCol: { style: {width:'calc(100% - 140px)'}},
        layout: "inline",
        labelAlign: "right",
        hideRequiredMark: false,
        formData: {
            ypBatch:'',
            tqwId:'',
            recordWeight:'',
            sfBalanceWeight:'',
            storageRecordWeight:''

        },
        rules: {},
    };
    //品名选择
    materialNameChange(v){
        console.log(v);
        
        let find = this.searchSource.materialNameList.find(item=>item.name === v)
        if(find){
          this.materialCode = find.code
          if(this.tabName==="饮片库存表"){
                this.formOption.formData.ypId = find.id
                this.formOption.formData.materialCode = find.code
                this.formOption.formData.ypValidity = find.validDay
          }
          if(this.tabName==="收粉库存表"){
                this.formOption.formData.tqwId = find.id
                this.formOption.formData.sfValidity = find.validDay
                this.formOption.formData.gsValidity = find.validDay
                this.formOption.formData.zhyValidity = find.validDay
          }
          if(this.tabName==="总混二库存表"){
                this.formOption.formData.zlId = find.id
                this.formOption.formData.zheValidity = find.validDay
                this.formOption.formData.zlValidity = find.validDay
          }
        }
        this.loading = false
    }
    //提交
    modalOk(){
        (this.$refs.formModel as any).validate(async (valid: boolean) => {
            // 自动补充操作人和部门
            const userInfo = getStorageUserInfo();
            if(this.tabName === '饮片库存表'){
                this.formOption.formData.ypEdit = userInfo.name;
                this.formOption.formData.ypDepartment = userInfo.organizationName;
            }else if(this.tabName === '收粉库存表'){
                this.formOption.formData.sfEdit = userInfo.name;
                this.formOption.formData.sfDepartment = userInfo.organizationName;
            }else if(this.tabName === '过筛库存表'){
                this.formOption.formData.gsEdit = userInfo.name;
                this.formOption.formData.gsDepartment = userInfo.organizationName;
            }else if(this.tabName === '总混一库存表'){
                this.formOption.formData.zhyEdit = userInfo.name;
                this.formOption.formData.zhyDepartment = userInfo.organizationName;
            }else if(this.tabName === '总混二库存表'){
                this.formOption.formData.zheEdit = userInfo.name;
                this.formOption.formData.zheDepartment = userInfo.organizationName;
            }else if(this.tabName === '制粒库存表'){
                this.formOption.formData.zleEdit = userInfo.name;
                this.formOption.formData.zlDepartment = userInfo.organizationName;
            }
            console.log('type:',this.tabName.length === 5 ? this.formOption.formData.type = this.tabName.substring(0,2):this.formOption.formData.type = this.tabName.substring(0,3));
            
            if (valid) {
                this.modalOption.loading = true;
                try {
                    if(this.editId){
                        this.tabName.length === 5 ? this.formOption.formData.type = this.tabName.substring(0,2):this.formOption.formData.type = this.tabName.substring(0,3)
                        this.formOption.formData.id = this.editId
                        const res = await nextApi.addHalfRawProduct(this.formOption.formData)
                        this.$message.success(res.msg)
                        this.$emit("closeModal")
                    }else{
                        this.tabName.length === 5 ? this.formOption.formData.type = this.tabName.substring(0,2):this.formOption.formData.type = this.tabName.substring(0,3)
                        const res = await nextApi.addHalfRawProduct(this.formOption.formData)
                        this.$message.success(res.msg)
                        this.$emit("closeModal")
                    }
                }finally {
                    this.modalOption.loading = false;
                }
            }
        });
    }
    /**
     * @description 查询一些数据
     * **/
    //查询平衡表
    debouncedGetBalanceTableData: any = null
    getBalanceData(){
        if (!this.debouncedGetBalanceTableData) {
            this.debouncedGetBalanceTableData = tools.debounce(this.getBalanceTableData.bind(this), 500);
        }
        this.debouncedGetBalanceTableData();
    }
    async getBalanceTableData(){
            
            this.loading = true
            let batch:string,type:string,params:any
            if(this.tabName==="收粉库存表" || this.tabName==="过筛库存表" || this.tabName==="总混一库存表"){
                batch = this.formOption.formData.tqwBatch
                type = '提取'
            }else if(this.tabName==="总混二库存表" || this.tabName==="制粒库存表"){
                batch = this.formOption.formData.zlBatch
                type = '制剂'
            }else{
                batch = ''
                type = ''
            }
            params = {
                batch,
                type
            }
            const res = await nextApi.getBalanceTableData(params)
            if(res.code === 200 && res.data){
                if(this.tabName==="收粉库存表"){
                    this.formOption.formData.recordWeight = res.data.sfProdNum
                }
                if(this.tabName==="过筛库存表"){
                    this.formOption.formData.recordWeight = res.data.gsProdNum
                }
                if(this.tabName==="总混一库存表"){
                    this.formOption.formData.recordWeight = res.data.hyProdNum
                }
                if(this.tabName==="总混二库存表"){
                    this.formOption.formData.recordWeight = res.data.prodNum
                }
                if(this.tabName==="制粒库存表"){
                    this.formOption.formData.recordWeight = res.data.klProdNum
                }
            }else{
                this.formOption.formData.recordWeight = ''
            }
            this.loading = false

    }
    async getMaterialNameList(materialType:string,type:string){
        this.loading = true
        let params = {
                standard:this.formOption.formData.standard,
                materialType,
                type,
                page:1,
                pageSize:10000,
            }
        const res = await nextApi.getMaterialList(params)
        this.searchSource.materialNameList = res.data.records
        console.log( this.searchSource.materialNameList)
        this.loading = false
    }
    async getZhyList(){
            let params = {
                pageSize: 99999,
                page: 1,
                type: '提取物',
                type2:'总混一',
            }
            const res = await nextApi.getHalfRawPorduct(params)
            this.searchSource.zhyList = res.data?.records
    }
    async standardTypeChange(){
        if(this.tabName==="饮片库存表"){
            await this.getMaterialNameList('饮片','半成品')
        }else if(this.tabName==="收粉库存表" || this.tabName==="过筛库存表" || this.tabName==="总混一库存表"){
            await this.getMaterialNameList('提取物','半成品')
        }else if(this.tabName==="总混二库存表"){
            await this.getMaterialNameList('','待包装成品')
            await this.getZhyList()
        }else{
            await this.getMaterialNameList('制粒','半成品') 
        }
    }
    closeModal() {
        this.$emit("closeModal");
    }
    // 库存调整记录表头
    adjustColumns = [
        { 
            title: '序号',
            dataIndex: 'index',
            key: 'index', 
            align: 'center',
            customRender:(text:string,record:any,index:number)=>index+1 
        },
        { 
            title: '重量类型',
            dataIndex: 'weightType',
            key: 'weightType', 
            align: 'center',
            customRender:(text:string,record:any,index:number)=>text == '2' ? '记录重量':'入库重量'
        },
        { 
            title: '调整类型',
            dataIndex: 'operate',
            key: 'operate',
            align: 'center',
            customRender:(text:string,record:any,index:number)=>{
                return record.operate==1 ? '正常出库':record.operate==2 ? '其他出库':'正常入库'
           }
         },
        { title: '调整时间', dataIndex: 'outTime', key: 'outTime', align: 'center' },
        { title: '重量', dataIndex: 'weight', key: 'weight', align: 'center' },
        { title: '调整原因', dataIndex: 'reason', key: 'reason', align: 'center' },
        { title: '实时结存', dataIndex: 'nowStorage', key: 'nowStorage', align: 'center' },
    ];
    
    get correctName(){
        if(this.tabName === '饮片库存表'){
            return "饮片" 
        }else if(this.tabName === '收粉库存表' || this.tabName === '过筛库存表' || this.tabName === '总混一库存表'){
            return "提取物" 
        }else{
            return "颗粒"
        }
    }
    @Watch('formOption.formData.prodType')
    @Watch('materialCode')
    @Watch('formOption.formData.prodNo')
    change(){
        const { prodType, prodNo } = this.formOption.formData;
        if (prodType && this.materialCode && prodNo) {
            this.formOption.formData.ypBatch = `${prodType}${this.materialCode}${prodNo}`;
        }
    }
   

    // 监听件数、单位、零件变化
    @Watch('formOption.formData.gsNum')
    @Watch('formOption.formData.gsSpec')
    @Watch('formOption.formData.gsPart')
    onGsParamsChange() {
        const {gsNum, gsSpec, gsPart} = this.formOption.formData;
        if (gsNum && gsSpec) {
            const calculatedWeight = Number(gsNum) * Number(gsSpec) + Number(gsPart || 0);
            this.formOption.formData.gsInWeight = calculatedWeight;
        }
    }

    // 监听制粒件数、单位、零件变化
    @Watch('formOption.formData.zlNum')
    @Watch('formOption.formData.zlSpec')
    @Watch('formOption.formData.zlPart')
    onZlParamsChange() {
        const {zlNum, zlSpec, zlPart} = this.formOption.formData;
        if (zlNum && zlSpec) {
            const calculatedWeight = Number(zlNum) * Number(zlSpec) + Number(zlPart || 0);
            this.formOption.formData.zlInWeight = calculatedWeight;
        }
    }
    get gsInWeight() {
        return this.formOption.formData.gsInWeight || 0;
    }

    get zlInWeight() {
        return this.formOption.formData.zlInWeight || 0;
    }
    mounted() {
    }

    beforeDestroy() {
        if (this.debouncedGetBalanceTableData) {
            this.debouncedGetBalanceTableData = null;
        }
    }
}
</script>
<style scoped lang="scss">
.container {
    padding: 0;
    .MR-10 {
        margin-right: 10px;
    }
    .inputs {
        width: 200px;
    }
    .inputs-with-qus{
        width: 173px;
    }
    .tool-bar {
        margin-bottom: 20px;
    }
    .table {
        margin-top: 20px;
    }
    // 新增优化样式
    .ant-input,
    .ant-select,
    .ant-calendar-picker,
    .ant-picker {
        width: 200px !important;
        min-width: 120px;
        height: 32px;
        line-height: 32px;
    }
    .ant-form-item {
        margin-bottom: 16px;
    }
    .ant-card {
        margin-bottom: 16px;
    }
    .ant-form-item-label > label {
        height: 32px;
        line-height: 32px;
        font-size: 14px;
    }
    .ant-btn {
        height: 32px;
        line-height: 32px;
        min-width: 60px;
        padding: 0 16px;
        margin-right: 4px;
    }
    .inspection-row {
        display: flex;
        align-items: center;
        .ant-form-item {
            margin-bottom: 0;
        }
    }
}
</style>
